package com.ruoyi.attendance.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.attendance.domain.AttendanceStatusVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.attendance.domain.SignInfoView;
import com.ruoyi.attendance.service.ISignInfoViewService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考勤记录Controller
 * 
 * @author Weiney
 * @date 2025-04-12
 */
@RestController
@RequestMapping("/attendance/signRecord")
@Api(tags = "考勤管理模块")
public class SignInfoViewController extends BaseController
{
    @Autowired
    private ISignInfoViewService signInfoViewService;

    /**
     * 查询考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('attendance:signRecord:list')")
    @GetMapping("/list")
    @ApiOperation("查询考勤记录列表")
    public TableDataInfo list(SignInfoView signInfoView)
    {
        startPage();
        signInfoView.setUserId(getUserId());
        signInfoView.setDeptId(getDeptId());
        List<SignInfoView> list = signInfoViewService.selectSignInfoViewList(signInfoView);
        return getDataTable(list);
    }

    /**
     * 考勤信息统计
     */
    @PreAuthorize("@ss.hasPermi('attendance:signRecord:statistic')")
    @GetMapping("/listForStatistic")
    @ApiOperation("考勤信息统计")
    public AjaxResult listForStatistic(SignInfoView signInfoView)
    {
        signInfoView.setUserId(getUserId());
        signInfoView.setDeptId(getDeptId());
        List<SignInfoView> list = signInfoViewService.selectSignStatistic(signInfoView);
        return AjaxResult.success(list);
    }

    /**
     * 签到页面的考勤列表，只能查看本人的
     * @param signInfoView
     * @return
     */
    @PreAuthorize("@ss.hasPermi('attendance:signRecord:listForOwn')")
    @GetMapping("/listForOwn")
    @ApiOperation("查询本人考勤记录列表")
    public TableDataInfo listForOwn(SignInfoView signInfoView){
        startPage();
        signInfoView.setUserId(getUserId());
        List<SignInfoView> list = signInfoViewService.selectSignInfoViewListForOwn(signInfoView);
        return getDataTable(list);
    }

    /**
     * 导出考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('attendance:signRecord:export')")
    @Log(title = "考勤记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SignInfoView signInfoView)
    {
        List<SignInfoView> list = signInfoViewService.selectSignInfoViewList(signInfoView);
        ExcelUtil<SignInfoView> util = new ExcelUtil<SignInfoView>(SignInfoView.class);
        util.exportExcel(response, list, "考勤记录数据");
    }

    /**
     * 获取考勤记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('attendance:signRecord:query')")
    @GetMapping(value = "/{signId}")
    public AjaxResult getInfo(@PathVariable("signId") Long signId)
    {
        return success(signInfoViewService.selectSignInfoViewBySignId(signId));
    }

    /**
     * 新增考勤记录
     */
    @PreAuthorize("@ss.hasPermi('attendance:signRecord:add')")
    @Log(title = "考勤记录", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增考勤记录")
    public AjaxResult add(@RequestBody SignInfoView signInfoView)
    {
        signInfoView.setUserId(getUserId());
        signInfoView.setDeptId(getDeptId());
        int count = signInfoViewService.insertSignInfoView(signInfoView);
        if (count > 0) {
            return toAjax(count);
        } else {
            return AjaxResult.error("考勤异常，请联系管理员！");
        }
    }

    /**
     * 获取考勤记录状态
     * @return
     */
    @PreAuthorize("@ss.hasPermi('attendance:signRecord:getStatus')")
    @GetMapping(value = "/getStatus")
    @ApiOperation("获取考勤记录状态")
    public AjaxResult getAttendanceStatus(){
        Long userId = getUserId();
        Long deptId = getDeptId();
        AttendanceStatusVo attendanceStatusVo = signInfoViewService.selectAttendanceStatus(userId, deptId);
        return success(attendanceStatusVo);
    }

    /**
     * 修改考勤记录
     */
    @PreAuthorize("@ss.hasPermi('attendance:signRecord:edit')")
    @Log(title = "考勤记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SignInfoView signInfoView)
    {
        return toAjax(signInfoViewService.updateSignInfoView(signInfoView));
    }

    /**
     * 删除考勤记录
     */
    @PreAuthorize("@ss.hasPermi('attendance:signRecord:remove')")
    @Log(title = "考勤记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{signIds}")
    public AjaxResult remove(@PathVariable Long[] signIds)
    {
        return toAjax(signInfoViewService.deleteSignInfoViewBySignIds(signIds));
    }
}
